Skip to content

Show make_constructor() a few times in extension vignette #6542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #6524.

It replaces some constructor code with the make_constructor() shortcut. This also leaves 1 instance of when not to use a constructor when you want to bake-in the 'stat' for example.

@teunbrand
Copy link
Collaborator Author

Suggestion from @EvaMaeRey: should we change make_constructor() to write_constructor()? I kind of like it because it writes a function for you so that you don't have to.

@teunbrand teunbrand added this to the ggplot2 4.0.0 milestone Jul 7, 2025
@EvaMaeRey
Copy link
Contributor

Hi Teun, Exciting to see the new function in action in the vignette!

I think the geom_chull case would not be one where I'd opt to write out the constructor instead of using make_constructor/write_constructor. I think the convention is arguably to expose the stat (even though it would be very strange to see a user switch this out).

Actually, in the ggplot2 book's example of geom_chull the stat argument is exposed, https://ggplot2-book.org/extensions.html#sec-modifying-geom-defaults, and I think this represents convention more than a locked-in stat (and will enable the super terse make_constructor move! ); maybe more representative of conventions?

And in the book case study, where geom_spring is first defined as a near alias to stat_spring, stat argument is also exposed https://ggplot2-book.org/ext-springs.html#constructors 21.2.4 Constructors And writing these two functions is a place where make_constructor/write_constructor really shines (otherwise scaffolding code 2x).

stat_spring <- make_constructor(StatSpring, geom = "path") # write a constructor instead of geom_path(stat = StatSpring)
geom_spring <- write_constructor(GeomPath, stat = "spring") # and just because 'ggplot2 users are accustomed to adding geoms, not stats, when building up a plot'

From base ggplot2, geom_count is another example where the stat is exposed, even though the function name kind of refers to the stat (StatSum)

ggplot2::geom_count

function (mapping = NULL, data = NULL, stat = "sum", position = "identity",
..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
{
layer(mapping = mapping, data = data, geom = "point", stat = stat,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list2(na.rm = na.rm, ...))
}

While, I think there probably should be a couple of examples of write-it-out construction, I don't think geom_chull is the right spot to do it (so the vignette should probably follow the convention that geom_*() functions in general expose stat argument to users - as weird it would be for users to actually switch the stat out).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Showcase make_constructor() in extension vignette
2 participants